<?php

/**
 *  后台用户控制器
 * @author xiaojy  xiaojy@wanthings.com
 * @copyright 成都网信天成科技有限公司, 2016, http://www.wanthings.com
 */
class UserController extends AdminController
{
    public function actionList()
    {

        $filter = array();
        $filter['name'] = Yii::app()->request->getQuery('name'); // 姓名
        $filter['mobile'] = Yii::app()->request->getQuery('mobile'); // 电话
        $filter['apply'] = Yii::app()->request->getQuery('apply');
        $filter['keyword'] = Yii::app()->request->getQuery('keyword');
        $filter['only_new'] = $new_apply;
        $filter['sid'] = Yii::app()->request->getQuery('sid');
        if (!empty($ids)) {
            $filter['ids'] = $ids;
        }
        $min_time = Yii::app()->request->getQuery('min_time');
        $max_time = Yii::app()->request->getQuery('max_time');
        $filter['min_time'] = DateComponent::strToTime($min_time);
        $filter['max_time'] = DateComponent::strToTime($max_time);
        $filter['page'] = Yii::app()->request->getQuery('page', 1);
        $filter['page_size'] = Yii::app()->request->getQuery('page_size', 20);
        $filter['style'] = Yii::app()->request->getQuery('style', 'id desc'); // 排序方式

        $result = User::model()->getList($filter);

        $this->render('list', array('user' => $result, 'filters' => $filter));
    }


    public function actionAdd()
    {
        $user = array();
        $user['sid'] = '';
        $user['mobile'] = '';
        $user['name'] = '';
        $user['gender'] = '';
        $user['nickname'] = '';
        $user['open_bank'] = '';
        $user['bank_card'] = '';
        $user['idcard'] = '';
        $user['idcard_photo'] = '';

        if (Yii::app()->request->isPostRequest) {
            $info = Yii::app()->request->getPost('user');
            if (!empty($info['mobile'])) {
                $user = User::model()->add($info);
                if ($user['errno'] == 0) {

                    $this->showSuccess('添加成功！', $this->createUrl('user/list'));
                } else {
                    $this->showError('添加失败', $this->referer);
                }
            } else {
                $this->showError('手机号必填!', $this->createUrl('user/add'));
            }
        } else {
            $this->render('add', array('user' => null));
        }
    }


    /**
     *删除用户
     * @param sid 用户sid
     * @return
     *
     */
    public function actionDelete()
    {
        $sid = Yii::app()->request->getQuery('sid');
        $re = User::model()->deleteBySid($sid);
        if ($result) {
            $this->showSuccess('用户删除成功', $this->createUrl('/admin/user/list'));
        } else {
            $this->showError('用户删除失败', $this->createUrl('/admin/user/list'));
        }
    }


    /**
     * 用户ajax验证电话号码
     * @return json error
     */
    public function actionCheckmobile()
    {
        $mobile = Yii::app()->request->getPost('mobile');
        $result = User::model()->checkMobile($mobile);
        if ($result > 0) {
            $error = array('errno' => '1', 'errmsg' => '输入的电话号码已经存在');
        } else {
            $error = array('errno' => '0');
        }
        echo CJSON::encode($error);
        exit();
    }

    //启用禁用
    public function actionStatus()
    {
        $sid = Yii::app()->request->getQuery('sid');
        $temp = User::model()->getStatus($sid);
        if ($temp['errno'] == 0) {
            $status = $temp['result'];
            if ($status == User::STATUS_NORMAL) {

                $re = User::model()->setStatus($sid, User::STATUS_DISABLE);
                if ($re['errno'] == 0) {
                    $this->showSuccess('已禁用', $this->referer);
                } else {
                    $this->showError('禁用失败', $this->referer);
                }
            } else {

                $re = User::model()->setStatus($sid, User::STATUS_NORMAL);
                if ($re['errno'] == 0) {
                    $this->showSuccess('已启用', $this->referer);
                } else {
                    $this->showError('启用失败', $this->referer);
                }
            }
        } else {
            $this->showMessage($temp['errmsg'], $this->referer);
        }
    }


    //重置密码
    public function actionresetPwd()
    {
        $user['sid'] = Yii::app()->request->getQuery('sid');
        if (Yii::app()->request->getIsPostRequest()) {
            $pwd = Yii::app()->request->getPost('Passwd');
            if ($pwd['new'] == $pwd['repeat']) {
                $user['password'] = $pwd['new'];
                $re = User::model()->updateUser($user);
                if (!empty($re)) {
                    $this->showSuccess('密码重置成功', $this->createUrl('user/list'));
                } else {
                    $this->showError('密码重置失败', $this->createUrl('user/list'));
                }
            } else {
                $this->showError('两次输入的密码不一致!', $this->createUrl('user/list'));
            }
        }
        $this->render('reset');
    }

    /**
     * 用户导出
     * @return [type] [description]
     */
    public function actionExport()
    {
        set_time_limit(0);
        $filter = array();
        $filter['start_time'] = Yii::app()->request->getQuery('start_time', strtotime(date('Y-m-d', time())));
        $filter['end_time'] = Yii::app()->request->getQuery('end_time', strtotime(date('Y-m-d', time())) + 86400);
        $shop_list = Shop::model()->getSaleInfo($filter);
        $list = array();
        if (!empty($shop_list['data'])) {
            $list = $shop_list['data'];
        }
        if (empty($list)) {
            $this->showError("没有数据可导出", $this->referer);
        }
        $data = $list;
        require_once Yii::app()->basePath . "/extensions/PHPExcel_1.8.0/Classes/PHPExcel.php";
        //创建excel对象
        $excel = new PHPExcel();
        //第一行
        $excel->getActiveSheet()->mergeCells('A1:T1');
        $excel->getActiveSheet()->setCellValue('A1', '销售数据');
        //第二行
        $excel->getActiveSheet()->mergeCells('A2:E2');
        $excel->getActiveSheet()->setCellValue('A2', '统计销售数据');
        $excel->getActiveSheet()->mergeCells('F2:K2');
        $excel->getActiveSheet()->setCellValue('F2', ' ');
        //第三行
        $letter = array('A', 'B', 'C', 'D', 'E', 'F');
        $tableheader = array('名称', '销售额', '优惠券', '包装费', '结算额', '订单量');
        //填充表头信息
        for ($i = 0; $i < count($tableheader); $i++) {
            $excel->getActiveSheet()->setCellValue("$letter[$i]3", "$tableheader[$i]");
        }

        //填充表格，第四行开始
        for ($i = 4; $i <= count($data) + 3; $i++) {
            $j = 0;
            $row = 4;
            foreach ($data as $k => $v) {
                $start_row = $row;
                $excel->getActiveSheet()->setCellValueExplicit("A$row", $v['name']); // 订单号  以文本格式存入
                $excel->getActiveSheet()->setCellValue("B$row", $v['sale_amount']); // 下单时间
                $excel->getActiveSheet()->setCellValue("C$row", $v['coupon_amount']); // 收货时间
                $excel->getActiveSheet()->setCellValue("D$row", $v['packing_amount']); // 收货时间
                $excel->getActiveSheet()->setCellValue("E$row", $v['sale_amount'] - $v['coupon_amount']); // 消费会员姓名
                $excel->getActiveSheet()->setCellValue("F$row", $v['order_count']); // 消费会员姓名
                $row++;
            }
        }
        //文件名
        $file_name = '导出的数据';
        //创健excel输入对象
        $write = new PHPExcel_Writer_Excel5($excel);
        //清除缓存区，避免乱码
        ob_end_clean();
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="' . $file_name . '.xls"');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
    }


}
